﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;
using System.ComponentModel;
using System.Collections.ObjectModel;
using Microsoft.Win32;

namespace Clipper.DataManage
{
    /// <summary>
    /// WinDataManage.xaml 的交互逻辑
    /// </summary>
    public partial class WinDataManage : BaseWindow
    {
        #region 变量

        DetectItems mDetectItemsBll =new BLL.DetectItems();
        ResultReference mResultReferBll = new BLL.ResultReference();
        QCReference mQCBll = new BLL.QCReference();
        Reagent mReagentBll = new BLL.Reagent();
        DetectVariety mVarietyBll = new DetectVariety();
        
        Model.DetectItems mDetectItems=null;
        Model.Reagent mReagent = null;
        Model.QCReference mQCModel = null;
        Model.ResultReference mResultReference = null;
        Model.DetectVariety mVariety = null;

        ObservableCollection<Model.DetectItems> mDetectItemsList = null;
        ObservableCollection<Model.Reagent> mReagentList = null;
        ObservableCollection<Model.QCReference> mQCModelList = null;
        ObservableCollection<Model.ResultReference> mResultReferenceList = null;
        ObservableCollection<Model.DetectVariety> mVarietyList = null;
        #endregion

        #region 数据绑定 调用

        public WinDataManage()
        {
            InitializeComponent();
        }
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            DataBind();
        }
        public void DataBind()
        {
            BackgroundWorker worker = new BackgroundWorker();
            worker.DoWork += (ss, ee) =>
                {
                    mDetectItemsList = mDetectItemsBll.GetFullDetectItemsInfoList("");
                    mReagentList = mReagentBll.GetModelList("");
                    mQCModelList = mQCBll.GetFullDetectItemsInfoList("");
                    mResultReferenceList = mResultReferBll.GetFullDetectItemsInfoList("");
                    mVarietyList = mVarietyBll.GetModelList("");
                };
            worker.RunWorkerCompleted += (ss, ee) =>
                {
                    datDetectItems.ItemsSource = mDetectItemsList;
                    datReagent.ItemsSource = mReagentList;
                    datQCReference.ItemsSource = mQCModelList;
                    datResultReference.ItemsSource = mResultReferenceList;
                    datVarietySet.ItemsSource = mVarietyList;
                };
            worker.RunWorkerAsync();
            //UIHelper.InitDataGrid(mDetectItemsBll.GetFullDetectItemsInfoList, datDetectItems).RunWorkerAsync();
        }
        #endregion

        #region //Add方法
        private bool? inpQC_AddEvent()
        {
            WinQCReferenceInput winQcReferenceInput = WindowFactory.CreateInstance<WinQCReferenceInput>();
            this.BaseServiceProvider.InputName = Model.InputEnum.Add;
            winQcReferenceInput.Init(this.BaseServiceProvider);
            return winQcReferenceInput.ShowDialog();
        }
        private bool? inpResultReference_AddEvent()
        {
            WinResultReferenceInput winItem = WindowFactory.CreateInstance<WinResultReferenceInput>();
            this.BaseServiceProvider.InputName = Model.InputEnum.Add;
            winItem.Init(this.BaseServiceProvider);
            return winItem.ShowDialog();
        }

        private bool? inpBase_AddEvent()
        {
            WinDetectItemsInput winItem = WindowFactory.CreateInstance<WinDetectItemsInput>();
            this.BaseServiceProvider.InputName = Model.InputEnum.Add;
            winItem.Init(this.BaseServiceProvider);
            return winItem.ShowDialog();
        }

        private bool? inpReagent_AddEvent()
        {
            WinReagentInput winItem = WindowFactory.CreateInstance<WinReagentInput>();
            this.BaseServiceProvider.InputName = Model.InputEnum.Add;
            winItem.Init(this.BaseServiceProvider);
            return winItem.ShowDialog();
        }
        private bool? inpVarietySet_AddEvent()
        {
            WinVarietyInput winVariety = new WinVarietyInput();
            BaseServiceProvider.InputName = Model.InputEnum.Add;
            winVariety.Init(BaseServiceProvider);
            return winVariety.ShowDialog();
        }
        #endregion

        #region Delete方法
        private bool? inpReagent_DeleteEvent()
        {
            bool result = false;
            if (UIHelper.ShowKeyAskMessage("IsDelete"))
            {
                if (mReagent != null)
                {
                    result = mReagentBll.Delete(mReagent.reagentID);
                    UIHelper.SuccessOrFailTip(result,"DeleteTip");
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoDataTipe");
                }
            }
            return result;
        }

        private bool? inpBase_DeleteEvent()
        {
            bool result = false;
            if (UIHelper.ShowKeyAskMessage("IsDelete"))
            {
                if (mDetectItems != null)
                {
                    result = mDetectItemsBll.Delete(mDetectItems.detectItem);
                    UIHelper.SuccessOrFailTip(result,"DeleteTip");
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoDataTipe");
                }

            }
            return result;
        }

        private bool? inpResultReference_DeleteEvent()
        {
            bool result = false;
            if (UIHelper.ShowKeyAskMessage("IsDelete"))
            {
                if (mResultReference != null)
                {
                    result = mResultReferBll.Delete(mResultReference.DetectItem);
                    UIHelper.SuccessOrFailTip(result,"DeleteTip");
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoDataTipe");
                }

            }
            return result;
        }

        private bool? inpQC_DeleteEvent()
        {
            bool result = false;
            if (UIHelper.ShowKeyAskMessage("IsDelete"))
            {
                if (mQCModel != null)
                {
                    result = mQCBll.Delete(mQCModel.DetectItem);
                    UIHelper.SuccessOrFailTip(result, "DeleteTip");
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoDataTipe");
                }
            }
            return result;
        }

        private bool? inpVarietySet_DeleteEvent()
        {
            bool result = false;
            if (UIHelper.ShowKeyAskMessage("IsDelete"))
            {
                if (mVariety != null)
                {
                    result = mVarietyBll.Delete(mVariety.itemTypeID);
                    UIHelper.SuccessOrFailTip(result, "DeleteTip");
                }
                else
                {
                    UIHelper.ShowKeyMessage("NoDataTipe");
                }
            }
            return result;
        }

        #endregion

        #region //Update方法

        private bool? inpBase_ModifyEvent()
        {
            if (mDetectItems != null)
            {
                WinDetectItemsInput winItem = WindowFactory.CreateInstance<WinDetectItemsInput>();
                BaseServiceProvider.DataModel = mDetectItems;
                BaseServiceProvider.InputName = Model.InputEnum.Update;
                winItem.Init(this.BaseServiceProvider);
                return winItem.ShowDialog();
            }
            else
            {
                UIHelper.ShowKeyMessage("NoDataTipe");
            }
            return false;
        }

        private bool? inpResultReference_ModifyEvent()
        {
            if (mResultReference != null)
            {
                WinResultReferenceInput winItem = WindowFactory.CreateInstance<WinResultReferenceInput>();
                BaseServiceProvider.DataModel = mResultReference;
                BaseServiceProvider.InputName = Model.InputEnum.Update;
                winItem.Init(this.BaseServiceProvider);
                return winItem.ShowDialog();
            }
            else
            {
                UIHelper.ShowKeyMessage("NoDataTipe");
            }
            return false;
        }

        private bool? inpQC_ModifyEvent()
        {
            if (mQCModel != null)
            {
                WinQCReferenceInput winItem = WindowFactory.CreateInstance<WinQCReferenceInput>();
                BaseServiceProvider.DataModel = mQCModel;
                BaseServiceProvider.InputName = Model.InputEnum.Update;
                winItem.Init(this.BaseServiceProvider);
                return winItem.ShowDialog();
            }
            else
            {
                UIHelper.ShowKeyMessage("NoDataTipe");
            }
            return false;
        }

        private bool? inpReagent_ModifyEvent()
        {
            if (mReagent != null)
            {
                WinReagentInput winItem = new WinReagentInput();
                BaseServiceProvider.DataModel = mReagent;
                BaseServiceProvider.InputName = Model.InputEnum.Update;
                winItem.Init(this.BaseServiceProvider);
                return winItem.ShowDialog();
            }
            else
            {
                UIHelper.ShowKeyMessage("NoDataTipe");
            }
            return false;
        }

        private bool? inpVarietySet_ModifyEvent()
        {
            if (mVariety != null)
            {
                WinVarietyInput winVariety = new WinVarietyInput();
                BaseServiceProvider.DataModel = mVariety;
                BaseServiceProvider.InputName = Model.InputEnum.Update;
                winVariety.Init(this.BaseServiceProvider);
                return winVariety.ShowDialog();
            }
            else
            {
                UIHelper.ShowKeyMessage("NoDataTipe");
            }
            return false;
        }
        #endregion
        
        #region //其他方法
        private void inpReagent_Loaded(object sender, RoutedEventArgs e)
        {

        }
        #endregion
         
        #region //选择事件
        private void datDetectItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mDetectItems = datDetectItems.CurrentItem as Model.DetectItems;
        }

        private void datResultReference_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mResultReference = datResultReference.CurrentItem as Model.ResultReference;
        }

        private void datQCReference_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mQCModel = datQCReference.CurrentItem as Model.QCReference; 
        }

        private void datReagent_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mReagent = datReagent.CurrentItem as Model.Reagent;
        }

        private void datVarietySet_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            mVariety = datVarietySet.CurrentItem as Model.DetectVariety;
        }
        #endregion
        
        #region 刷新事件
        private void inpReagent_RefreshEvent()
        {
            datReagent.ItemsSource = mReagentBll.GetModelList("");
        }
        private void inpBase_RefreshEvent()
        {
            datDetectItems.ItemsSource = mDetectItemsBll.GetFullDetectItemsInfoList("");
        }
        private void inpResultReference_RefreshEvent()
        {
            datResultReference.ItemsSource = mResultReferBll.GetFullDetectItemsInfoList("");
        }
        private void inpQC_RefreshEvent()
        {
            datQCReference.ItemsSource = mQCBll.GetFullDetectItemsInfoList("");
        }
        private void inpVarietySet_RefreshEvent()
        {
            datVarietySet.ItemsSource = mVarietyBll.GetModelList("");
        }
        #endregion

        private void itemImportExport_MouseUp(object sender, MouseButtonEventArgs e)
        {
            WinImportExport winimport = new WinImportExport();
            winimport.ShowDialog();
        }

        //private bool? inpResultReference_AddEvent()
        //{

        //}

        //private bool? inpBase_AddEvent()
        //{
        //}

        //private bool? inpBase_ModifyEvent()
        //{
        //}
    }
}  
 